﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Strive.DataAccess;
using OTA.Notification.Interface.IDAO.Dictionary;
using OTA.Notification.Entity.Dictionary;
using Strive.DataAccess.Base.Model.PageObject;
using Strive.DataAccess.Base.Model.SearcherObject;
using System.Data;

namespace OTA.Notification.DAO.Dictionary
{
    public class DictionaryDao:BaseDAOBean<OTA_DICTIONARY>,IDictionaryDao
    {
        public DictionaryDao() : base("connectionString") { }



        #region IDictionaryDao 成员

        public OTA_DICTIONARY getDictionary(int _id)
        {
            return base.GetObject(_id);
        }

        
        public OTA_DICTIONARY getDictionary(string _type, string _code, string _value)
        {
            IList<OTA_DICTIONARY> list = base.QueryList(base.Query.Constrain("PARENT_CODE").Equal(_code).And().Constrain("TYPE").Equal(_type).And().Constrain("VALUE").Equal(_value));
            if (list != null && list.Count > 0)
                return list[0];
            else
                return null;
        }

        public OTA_DICTIONARY getDictionary(string _type, string _code, string _value,string _id)
        {
            IList<OTA_DICTIONARY> list = base.QueryList(base.Query.Constrain("PARENT_CODE").Equal(_code).And().Constrain("TYPE").Equal(_type).And().Constrain("VALUE").Equal(_value).And().Constrain("id").NotEqual(_id));
            if (list != null && list.Count > 0)
                return list[0];
            else
                return null;
        }

        public string getName(string _type, string _code, string _value, string _languagetype)
        {
            try
            {
                DataTable dt = base.ExceDataTable("select dbo.f_GetContentName([NAME],'" + _languagetype + "') AS [Name] from ota_dictionary where [type]='" + _type + "' and parent_code='" + _code + "' and [value]='" + _value + "'");
                if (dt != null && dt.Rows.Count > 0)
                    return dt.Rows[0]["name"].ToString();
                else
                    return "";
            }
            catch
            {
                return string.Empty;
            }
        }

        public IList<OTA_DICTIONARY> getAllDictionary(string _code, string _type, string _languagetype)
        {
            IList<OTA_DICTIONARY> list = base.ExceSql("select code,[value],dbo.f_GetContentName([NAME],'" + _languagetype + "') AS [Name] from ota_dictionary where type='"+_type+"' and parent_code='"+_code+"'");
            
            if (list != null && list.Count > 0)
                return list;
            else
                return null;
        }

        public bool insertDictionary(OTA_DICTIONARY _dict)
        {
            if (base.Insert(_dict) > 0)
                return true;
            else
                return false;
        }

        public bool updateDictionary(OTA_DICTIONARY _dict)
        {
            if ( base.Update(_dict)> 0)
                return true;
            else
                return false;
        }

        public bool deleteDictionary(int _id)
        {
            if (base.Delete(base.Query.Constrain("ID").Equal(_id)) > 0)
                return true;
            else
                return false;
        }

        #endregion

        #region IBaseDAOBean<int,OTA_DICTIONARY> 成员

        public int DataDelete(OTA_DICTIONARY obj)
        {
            return base.Delete(obj);
        }

        public int DataInsert(OTA_DICTIONARY obj)
        {
            return base.Insert(obj);
        }

        public OTA_DICTIONARY DataSelect(int key)
        {
            return base.GetObject(key);
        }

        public int DataUpdate(OTA_DICTIONARY obj)
        {
            return base.Update(obj);
        }

        public IList<OTA_DICTIONARY> SelectAll()
        {
            return base.GetObjectList();
        }

        #endregion

        #region IDictionaryDao 成员


        public PageResult<OTA_DICTIONARY> getDictionaryPageList(BasePageSearch pageObject)
        {
            return base.QueryPageList(pageObject);
        }

        public string clearBadtree()
        {
            DataTable dt = base.ExceDataTableSP("clearbadtree");
            return dt.Rows[0][0].ToString();
        }

        #endregion
    }
}
